import { baseModule } from "@/base/module";
import { Player } from "@minecraft/server";
import { LazyLoader } from "@sapi-game/gameComponent/common/lazyLoader";
import { RegionTeamChooser } from "@sapi-game/gameComponent/region/regionTeamChooser";
import { GamePlayer, PlayerGroup, PlayerGroupSet } from "@sapi-game/gamePlayer";
import { CubeRegion, SphereRegion } from "@sapi-game/gameRegion/gameRegion";
import { Game, RegionTeamCleaner, TeamScoreBoard } from "@sapi-game/main";
import { DimensionIds } from "@sapi-game/utils/vanila-data";
import { BlockedConfigManager } from "../components/configManager";
import { BlockedInCombatStarter } from "../components/starter";
import { BlockedInCombat } from "../game";
import { BlockedTeamColors } from "../interface";

export class BlockedLobbyState extends baseModule.State {
    //队伍
    readonly teams = {
        red: new PlayerGroup(GamePlayer),
        blue: new PlayerGroup(GamePlayer),
        yellow: new PlayerGroup(GamePlayer),
        green: new PlayerGroup(GamePlayer),
    };
    readonly groupSet = new PlayerGroupSet(Object.values(this.teams));
    //大厅范围
    readonly lobbyRegion = new SphereRegion(
        DimensionIds.Overworld,
        { x: 868, y: -53, z: 945 },
        20
    );

    override onEnter(): void {
        this.addComponent(LazyLoader, {
            dimensionId: DimensionIds.Overworld,
            pos: this.lobbyRegion.center,
            onLoad: this.onLoad.bind(this),
            onUnload: this.onUnload.bind(this),
        });
    }

    onLoad(loader: LazyLoader) {
        loader.addComponent(TeamScoreBoard, {
            scoreboardName: "blocked_lobby",
            displayName: "惊天旷工团",
            teams: [
                { team: this.teams.red, prefix: "§c" },
                { team: this.teams.blue, prefix: "§b" },
                { team: this.teams.green, prefix: "§a" },
                { team: this.teams.yellow, prefix: "§e" },
            ],
        });

        loader.addComponent(RegionTeamChooser, {
            config: [
                {
                    team: this.teams.red,
                    region: new CubeRegion(
                        DimensionIds.Overworld,
                        { x: 868, y: -52, z: 952 },
                        { x: 867, y: -52, z: 951 }
                    ),
                    onJoin: (p) => {
                        p.sendMessage(`§c成功加入红队`);
                        this.onJoin(p.player);
                    },
                },
                {
                    team: this.teams.blue,
                    region: new CubeRegion(
                        DimensionIds.Overworld,
                        { x: 864, y: -52, z: 943 },
                        { x: 863, y: -52, z: 944 }
                    ),
                    onJoin: (p) => {
                        p.sendMessage(`§b成功加入蓝队`);
                        this.onJoin(p.player);
                    },
                },
                {
                    team: this.teams.green,
                    region: new CubeRegion(
                        DimensionIds.Overworld,
                        { x: 864, y: -52, z: 947 },
                        { x: 863, y: -52, z: 948 }
                    ),
                    onJoin: (p) => {
                        p.sendMessage(`§a成功加入绿队`);
                        this.onJoin(p.player);
                    },
                },
                {
                    team: this.teams.yellow,
                    region: new CubeRegion(
                        DimensionIds.Overworld,
                        { x: 866, y: -52, z: 940 },
                        { x: 865, y: -52, z: 939 }
                    ),
                    onJoin: (p) => {
                        p.sendMessage(`§e成功加入黄队`);
                        this.onJoin(p.player);
                    },
                },
            ],
        });
        loader.addComponent(RegionTeamCleaner, {
            region: this.lobbyRegion,
            teams: Object.values(this.teams),
        });
        loader
            .addComponent(BlockedInCombatStarter)
            .addComponent(BlockedConfigManager);
    }

    onJoin(player?: Player) {
        const sb = this.getComponent(TeamScoreBoard);
        sb.show();
        sb.refreshScoreBoard();
        player?.playSound("note.bell");
    }

    onUnload() {
        Object.values(this.teams).forEach((t) => t.clear());
    }

    canStartGameByPlayerCount() {
        const teams = Object.values(this.teams);
        const validTeams = teams.filter((t) => t.validSize >= 1).length;

        return Game.config.config.debugMode ? validTeams > 0 : validTeams >= 2;
    }

    start() {
        if (!this.canStartGameByPlayerCount()) {
            this.lobbyRegion.forEachPlayer((p) =>
                p.onScreenDisplay.setTitle("人数不足，已取消")
            );
        } else {
            Object.values(this.teams).forEach((t) => t.clearInvalid());
            const config = this.getComponent(BlockedConfigManager).getConfig();
            Game.manager.startGame(BlockedInCombat, {
                teams: Object.entries(this.teams) as [
                    BlockedTeamColors,
                    PlayerGroup
                ][],
                config: config,
            });
        }
        this.getComponent(LazyLoader).reload();
    }
}
